package uit.server.xml;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ArcXML {

	public String  getRequestSample(){
		String xmlString = "";
		try {
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			DocumentBuilder builder=dbf.newDocumentBuilder();

			Document doc=builder.newDocument();
			Element arcxml = doc.createElement ("ARCXML");
			arcxml.setAttribute("version","1.0");
			doc.appendChild(arcxml);

			Element request=doc.createElement("REQUEST");
			arcxml.appendChild(request);

			Element get_Image=doc.createElement("GET_IMAGE");
			request.appendChild(get_Image);

			Element properties=doc.createElement("PROPERTIES");
			get_Image.appendChild(properties);

			Element envelope=doc.createElement("ENVELOPE");
			properties.appendChild(envelope);
			envelope.setAttribute("minx","-122.5");
			envelope.setAttribute("miny","37.8");
			envelope.setAttribute("maxx","-122.4");
			envelope.setAttribute("maxy","37.9");


			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			 xmlString = sw.toString();
		} catch (DOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return xmlString;
	}

}
